package com.client.controller;

import com.Emplyment.result.Result;
import com.Emplyment.util.AliPay;
import com.Emplyment.vo.PayResultVo;
import com.client.service.MenberCenterService;
import com.itWaVel.feign.client.AliPaySuperUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/menbercenter")
@Slf4j
public class MenberCenterController {

    @Autowired
    private MenberCenterService menberCenterService;
    @Autowired
    private AliPaySuperUser aliPaySuperUser;

    @GetMapping("/getMenberInfo")
    public Result getMenberInfo(@RequestParam String username) {
        log.info("获取用户名为:{}的会员信息", username);
        Integer count = menberCenterService.getMenberInfo(username);
        log.info("查询到{}条会员信息", count);
        return Result.success(count);
    }

    @GetMapping("/menberAliPay")
    public Result<String> aliPay(@RequestParam String username, @RequestParam double amount) {
        log.info("用户名为:{}, 价格为:{}", username, amount);
        AliPay aliPay = menberCenterService.aliPay(username, amount);
        Result<String> result = aliPaySuperUser.aliPay(aliPay);
        log.info("支付宝支付地址为:{}", result.getData());
        return result;
    }

    @PostMapping("/paySuccess")
    public Result PaySuccess(@RequestBody PayResultVo payResultVo) {
        log.info("支付成功回调, payResultVo:{}", payResultVo);

        Integer count = menberCenterService.getMenberInfo(payResultVo.getUsername());
        if (count == 0) {
            menberCenterService.updataPayStatus(payResultVo);
            return Result.success(true);
        } else {
            return Result.success(false);
        }

    }

}
